package com.example.reviewdemo.login;

import androidx.appcompat.app.AppCompatActivity;
import androidx.room.Room;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.example.reviewdemo.MainActivity;
import com.example.reviewdemo.R;
import com.example.reviewdemo.bean.User;
import com.example.reviewdemo.constant.Constants;
import com.example.reviewdemo.database.UserDatabase;
import com.example.reviewdemo.password.UpdatePasswordActivity;
import com.example.reviewdemo.register.RegisterActivity;
import com.example.reviewdemo.utils.DateUtil;
import com.example.reviewdemo.utils.PasswordUtil;
import com.example.reviewdemo.utils.ToastUtil;

import java.util.Date;

public class LoginActivity extends AppCompatActivity implements View.OnClickListener {


    private TextView username, password, tv_register, tv_forget;
    private Button btn_login;

    private UserDatabase userDatabase;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        userDatabase = Room.databaseBuilder(this, UserDatabase.class, "user")
                .addMigrations()
                .allowMainThreadQueries()
                .build();

        initView();


    }

    @SuppressLint("WrongViewCast")
    private void initView() {
        username = findViewById(R.id.et_username);
        password = findViewById(R.id.et_password);
        tv_register = findViewById(R.id.tv_register);
        tv_forget = findViewById(R.id.tv_forget);
        btn_login = findViewById(R.id.login);
        btn_login.setOnClickListener(this);
        tv_register.setOnClickListener(this);
        tv_forget.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.login) {
            String username = this.username.getText().toString();
            String password = this.password.getText().toString();
            if (TextUtils.isEmpty(username) || TextUtils.isEmpty(password)) {
                ToastUtil.toastLong(this, Constants.FORM_NULL);
            } else {
                User user = userDatabase.userDao().findByUsername(username);
                if (user == null) {
                    ToastUtil.toastShort(this, Constants.USERNAME_NOT_EXIST);
                } else if (!TextUtils.equals(PasswordUtil.hashPassword(password), user.getPassword())) {
                    ToastUtil.toastLong(this, Constants.PASSWORD_ERROR);
                } else {
                    startActivity(new Intent(LoginActivity.this, MainActivity.class));
                    // 登录成功后执行的操作
                    SharedPreferences sharedPreferences = getSharedPreferences("loginPrefs", Context.MODE_PRIVATE);
                    SharedPreferences.Editor editor = sharedPreferences.edit();
                    editor.putBoolean("isLoggedIn", true);
                    editor.apply();
                }
            }
        }else if (v.getId() == R.id.tv_register){
            startActivity(new Intent(LoginActivity.this, RegisterActivity.class));
        }else if (v.getId() == R.id.tv_forget){
            startActivity(new Intent(LoginActivity.this, UpdatePasswordActivity.class));
        }

    }
}